<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
class Notice extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $type = $this->request->post('type','');
            $where['u.status'] = 'normal';
            $where['d.user_id'] = $this->auth->id;
            $name = '';
            switch ($type) {
                case 'follow':
                    $name = 'DocumentUserFollow';
                    break;
                case 'favorites':
                    $name = 'DocumentUserFavorites';
                    break;
                case 'flag':
                    $name = 'DocumentUserFlag';
                    break;
                case 'comment':
                    $name = 'DocumentUserComments';
                    break;
            }
            if($type = 'system' ){
                $list = Db::name('Order')->where('user_id',$this->auth->id)->where('pay_status',1)->order('id desc')->page($page,10)->select();
            }else{
                $list = Db::name($name)->alias('u')->join('wd_document d','d.id = u.document_id')->where($where)->order('u.id desc')->page($page,10)->select();
            }
            foreach ($list as $key => &$value) {
                $value['user'] = Db::name('User')->where('id',$value['user_id'])->find();
                $value['createtime'] = date('Y-m-d H:i',$value['createtime']);
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }

        return $this->view->fetch();
    }
}